package DMSXL._2024.BinaryTrees;

public class hasPathSum {
    public static void main(String[] args) {
        Integer[] nums = {1,2,3,};
        TreeNode root = CreateTree.createBinaryTree(nums,0);
        System.out.println(new hasPathSum().hasPathSum(root,1));
    }
    boolean has = false;
    public boolean hasPathSum(TreeNode root, int targetSum) {
        if(root == null){
            return false;
        }
        backTrack(root,targetSum,root.val);
        return has;
    }
    public void backTrack(TreeNode root, int targetSum, int now){
        if(targetSum == now && root.left == null && root.right == null){
            has = true;
        }
        if(root.left != null){
            backTrack(root.left,targetSum,now + root.left.val);
        }
        if(root.right != null){
            backTrack(root.right,targetSum,now + root.right.val);
        }
    }
}
